/*******************************************************************************
 * The MIT License (MIT)
 * 
 * Copyright (c) 2013 Triggertrap Ltd
 * Author Neil Davies
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ******************************************************************************/
package com.tdt.alarm.model.seekbar;

import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.tdt.alarm.R;
import com.tdt.alarm.controller.MainActivity;
import com.tdt.alarm.model.AlarmDBHelper;
import com.tdt.alarm.model.SetAlarm;
import com.tdt.alarm.model.Time;
import com.tdt.alarm.model.seekbar.SeekArc.OnSeekArcChangeListener;

/**
 * 
 * SimpleActivity.java
 * 
 * @author Neil Davies
 * 
 */
public class AlarmModify extends Activity {

	private SeekArc mSeekArcMin;
	private SeekArc mSeekArcHour;

	private TextView mSeekArcMinProgress;
	private TextView mSeekArcHourProgress;
	
	private Button btnAmPm;
	Button btnAdd;
	Button btnCancel;
	EditText edtLabel;
	
	AlarmDBHelper alarmDB;
	
	private boolean isAM;

	public boolean isAM() {
		return isAM;
	}

	public void setAM(boolean isAM) {
		this.isAM = isAM;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alarm_modify);

		mSeekArcMin = (SeekArc) findViewById(R.id.seekArcMin);
		mSeekArcHour = (SeekArc) findViewById(R.id.seekArcHour);

		mSeekArcMinProgress = (TextView) findViewById(R.id.seekArcMinProgress);
		mSeekArcHourProgress = (TextView) findViewById(R.id.seekArcHourProgress);
		
		btnAmPm = (Button)findViewById(R.id.btnAmPm);
		btnAdd = (Button) findViewById(R.id.btnAdd);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		edtLabel = (EditText) findViewById(R.id.edtLabel);

		isAM = true;
		mSeekArcMin.setmMax(60);
		mSeekArcMin.setArcRotation(0);
		mSeekArcMin.setStartAngle(0);
		mSeekArcMin.setSweepAngle(360);
		mSeekArcMin.setArcWidth(20);
		mSeekArcMin.setProgressWidth(5);
		mSeekArcMin.setRoundedEdges(true);
		mSeekArcMin.setTouchInSide(true);
		mSeekArcHour.setClockwise(true);

		mSeekArcHour.setmMax(12);
		mSeekArcHour.setArcRotation(0);
		mSeekArcHour.setStartAngle(0);
		mSeekArcHour.setSweepAngle(360);
		mSeekArcHour.setArcWidth(20);
		mSeekArcHour.setProgressWidth(5);
		mSeekArcHour.setRoundedEdges(true);
		mSeekArcHour.setTouchInSide(true);
		mSeekArcHour.setClockwise(true);
		
		alarmDB = new AlarmDBHelper(getApplicationContext());

		Intent intent = this.getIntent();	
		Bundle extras = getIntent().getExtras();
		final int iPos= extras.getInt("pos");
		
//		final List<SetAlarm> list = Utils.readFile();
		final List<SetAlarm> list = alarmDB.getAlarms();
		for(int i = 0;i <list.size();i++){
			if(i==iPos){
				int min=list.get(i).getTime().getMinute();
				int hour=list.get(i).getTime().getHour();
				
				if(hour>=12)
				{
					setAM(false);
					hour-=12;
					btnAmPm.setBackgroundResource(R.drawable.custom_button_moon);
				}
				if(!list.get(i).getLabel().toString().equals(" ")){
					edtLabel.setText(list.get(i).getLabel().toString());
				}
				else {
					edtLabel.setText(null);
				}
				mSeekArcMinProgress.setText(min+"");
				mSeekArcHourProgress.setText(hour+"");
				
				mSeekArcMin.setProgress(min);
				mSeekArcMin.setArcRotation(0);
				mSeekArcHour.setProgress(hour);
				mSeekArcHour.setArcRotation(0);
//				mSeekArcHour.setStartAngle(list.get(i).getTime().getHour());
//				mSeekArcMin.setStartAngle(list.get(i).getTime().getMinute());
			}
		}
		
		mSeekArcMin.setOnSeekArcChangeListener(new OnSeekArcChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekArc seekArc) {
			}

			@Override
			public void onStartTrackingTouch(SeekArc seekArc) {
			}

			@Override
			public void onProgressChanged(SeekArc seekArc, int progress,
					boolean fromUser) {
				mSeekArcMinProgress.setText(String.valueOf(progress % 60));
			}
		});

		mSeekArcHour.setOnSeekArcChangeListener(new OnSeekArcChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekArc seekArc) {
			}

			@Override
			public void onStartTrackingTouch(SeekArc seekArc) {
			}

			@Override
			public void onProgressChanged(SeekArc seekArc, int progress,
					boolean fromUser) {
				progress %= 12;
//				if (isAM) {
//					progress += 12;
//					progress %= 24;
//				}
				mSeekArcHourProgress.setText(String.valueOf(progress));
			}
		});

		btnAmPm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (v.getId() == btnAmPm.getId()) {
					if (isAM) {
						isAM = false;
						btnAmPm.setBackgroundResource(R.drawable.custom_button_moon);
					} else {
						isAM = true;
						btnAmPm.setBackgroundResource(R.drawable.custom_button_sunny);
					}
				}
			}
		});
		
		btnAdd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int hour;
				if(!isAM){
					hour = Integer.parseInt((String) mSeekArcHourProgress.getText())+12;
				}
				else {
					hour = Integer.parseInt((String) mSeekArcHourProgress.getText());
				}
				
				int min = Integer.parseInt((String) mSeekArcMinProgress.getText());
				Time time = new Time(hour,min,0);
				
				//set new valua
				SetAlarm alarmToEdit = list.get(iPos);
				alarmToEdit.setTime(time);
				String label=" "; 
				try{
				label =edtLabel.getText().toString();
				}
				catch(Exception ex){
					label =" ";
				}
				alarmToEdit.setLabel(label);
				//Update database
				alarmDB.updateAlarm(alarmToEdit);
//				for(int i =0;i<list.size();i++){
//					if(i==iPos){
//						list.get(i).setTime(time);
//						String label=" "; 
//						try{
//						label =edtLabel.getText().toString();
//						}
//						catch(Exception ex){
//							label =" ";
//						}
//						list.get(i).setLabel(label);
//					}
//				}
				Collections.reverse(list);
				// Utils.updateFile(list);
				Intent i = new Intent(AlarmModify.this, MainActivity.class);
				startActivity(i);
				finish();
			}
		});
		
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				ArrayList<SetAlarm> list = Utils.readFile();
//				Toast.makeText(getApplicationContext(),"cancel"+list.size(), Toast.LENGTH_LONG).show();
				Intent i = new Intent(AlarmModify.this, MainActivity.class);
				startActivity(i);
				finish();
			}
		});

	}

}
